import { ElMessage, MessageParams } from 'element-plus'
import { h } from 'vue'

export const Message = (options: MessageParams) => {
  const baseOpt: MessageParams = {
    plain: true,
    grouping: true,
  }

  Object.assign(baseOpt, options)

  baseOpt.message = h(
    'div',
    { style: 'color: black' },
    baseOpt.message || defaultMessage[baseOpt.type],
  )

  if (!baseOpt.icon) {
    baseOpt.icon = h('i', {
      class: EIcon[baseOpt.type],
      style: { fontSize: '18px' },
    }) as undefined //解决类型异常，不影响实际展示
  }

  ElMessage(baseOpt)
}

const EIcon = {
  success: 'bi bi-check-circle-fill',
  warning: 'bi bi-exclamation-circle-fill',
  info: 'bi bi-info-circle-fill',
  error: 'bi bi-x-circle-fill',
}

const defaultMessage = {
  success: '操作成功',
  error: '操作失败',
}
